********************************************************************************
***                                Preamble                                  ***
********************************************************************************
* Project: Surveys 1 & 2
* Procedure: Cleaning, figures and tests
* Author: Manu Muñoz-Herrera
* version 17
********************************************************************************
// SURVEY 1
clear all
macro drop _all
import excel survey1.xlsx
rename D progress
rename F finished
rename K bias1
rename L bias2
rename M vs_bias
keeporder progress finished bias1 bias2 vs_bias
gen id= _n
drop if id==1 | id==2
destring finished, replace
destring progress, replace
destring bias1, replace
destring bias2, replace
destring vs_bias, replace
drop if progress<=70
drop finished
gen category=1
replace category=2 if bias1==.
drop id progress
gen id = _n	
order id category bias1 bias2 vs_bias 	
gen bias=.
replace bias = bias1 if category==1
replace bias = bias2 if category==2
drop bias1 bias2
la var id "Observation id"
la var category "Order of responses in survey"
la var bias "How biased was B's decision"
la def biaslbl 1 "Biased" 2 "Somewhat biased" 3 "Neither biased nor unbiased" 4 "Somewhat unbiased" 5 "Unbiased"
la val bias biaslbl
la var vs_bias "Which case is more biased?"
la def vs_biaslbl 1 "In Case 1" 2 "In Case 2" 3 "No difference"
la val vs_bias vs_biaslbl
tabulate bias, generate(dbias)	
la var dbias1 "Biased"  
la var dbias2 "Somewhat biased" 
la var dbias3 "Neither biased nor unbiased" 
la var dbias4 "Somewhat unbiased" 
la var dbias5 "Unbiased"
tabulate vs_bias, generate(dvs_bias)	
la var dvs_bias1 "Case 1"  
la var dvs_bias2 "Case 2" 
la var dvs_bias3 "No difference" 		
gen bias1 = 0
replace bias1 = 1 if dbias1==1
replace bias1 = 1 if dbias2==1
la var bias1 "Biased"
gen bias2 = 0
replace bias2 = 1 if dbias3==1
la var bias2 "Neutral"	
gen bias3 = 0
replace bias3 = 1 if dbias4==1
replace bias3 = 1 if dbias5==1
la var bias3 "Unbiased"	
gen bias_test = .
replace bias_test = 1 if bias1 == 1
replace bias_test = 0 if bias2 == 1		
replace bias_test = -1 if bias3 == 1
gen dbias_test = .
replace dbias_test = 1 if dvs_bias1 == 1
replace dbias_test = 0 if dvs_bias2 == 1		
replace dbias_test = -1 if dvs_bias3 == 1
gen unbiased1 = 0
replace unbiased1 = 1 if dvs_bias1==1
la var unbiased1 "Case 1 (B)"
gen unbiased2 = 0
replace unbiased2 = 1 if dvs_bias3==1
la var unbiased2 "Equal"		
gen unbiased3 = 0
replace unbiased3 = 1 if dvs_bias2==1
la var unbiased3 "Case 2 (C)"	
gen bias_compare=0
replace bias_compare=-1 if vs_bias==1
replace bias_compare=1 if vs_bias==2
gen survey = 1
la var survey "Survey"
la def surveylbl 1 "S1" 2 "S2"
la val survey surveylbl
keeporder survey bias vs_bias dbias1 dbias2 dbias3 dbias4 dbias5 dvs_bias1 dvs_bias2 dvs_bias3 bias1 bias2 bias3 bias_test dbias_test bias_compare unbiased1 unbiased2 unbiased3
save survey1.dta, replace		
// SURVEY 2
import delimited using survey2.csv, clear
keep q1 q2
drop if q1==.
drop if q2==.
ren q1 bias
la var bias "How biased was B's decision"
la def biaslbl 1 "Biased" 2 "Somewhat biased" 3 "Neither biased nor unbiased" 4 "Somewhat unbiased" 5 "Unbiased"
la val bias biaslbl
ren q2 vs_bias
la var vs_bias "Q4. Which case is more biased?"
la def vs_biaslbl 1 "In Case 1" 2 "In Case 2" 3 "No difference"
la val vs_bias vs_biaslbl
tabulate bias, generate(dbias)	
la var dbias1 "Biased"  
la var dbias2 "Somewhat biased" 
la var dbias3 "Neither biased nor unbiased" 
la var dbias4 "Somewhat unbiased" 
la var dbias5 "Unbiased"
tabulate vs_bias, generate(dvs_bias)	
la var dvs_bias1 "Case 1"  
la var dvs_bias2 "Case 2" 
la var dvs_bias3 "No difference" 			
gen bias1 = 0
replace bias1 = 1 if dbias1==1
replace bias1 = 1 if dbias2==1
la var bias1 "Biased"
gen bias2 = 0
replace bias2 = 1 if dbias3==1
la var bias2 "Neutral"	
gen bias3 = 0
replace bias3 = 1 if dbias4==1
replace bias3 = 1 if dbias5==1
la var bias3 "Unbiased"	
gen bias_test = .
replace bias_test = 1 if bias1 == 1
replace bias_test = 0 if bias2 == 1		
replace bias_test = -1 if bias3 == 1
gen dbias_test = .
replace dbias_test = 1 if dvs_bias1 == 1
replace dbias_test = 0 if dvs_bias2 == 1		
replace dbias_test = -1 if dvs_bias3 == 1
gen bias_compare=0
replace bias_compare=-1 if vs_bias==1
replace bias_compare=1 if vs_bias==2
gen unbiased1 = 0
replace unbiased1 = 1 if dvs_bias1==1
la var unbiased1 "Case 1 (B)"
gen unbiased2 = 0
replace unbiased2 = 1 if dvs_bias3==1
la var unbiased2 "Equal"		
gen unbiased3 = 0
replace unbiased3 = 1 if dvs_bias2==1
la var unbiased3 "Case 2 (C)"	
gen survey = 2
la var survey "Survey"
la def surveylbl 1 "S1" 2 "S2"
la val survey surveylbl
keeporder survey bias vs_bias dbias1 dbias2 dbias3 dbias4 dbias5 dvs_bias1 dvs_bias2 dvs_bias3 bias1 bias2 bias3 bias_test dbias_test bias_compare unbiased1 unbiased2 unbiased3
save survey2.dta, replace
// COMBINING SURVEY DATASETS
use survey1.dta, clear
append using survey2.dta
save surveys.dta, replace
// FIGURES	
use surveys.dta, clear	
graph set window fontface default
set scheme s2colormanu
global color2plight = `""0 0 0 %85""' 
global color3plight = `""252 186 3""' 
global color2p = `""0 0 0""' 
global color3p = `""212 159 0""' 
global color3plight2 = `""0 131 143 %95""'
global color3p2 = `""0 131 143""'
global color2p2 = `""55 71 79""' 
collapse bias1 bias2 bias3 unbiased1 unbiased2 unbiased3, by(survey)		
graph bar (mean) bias1 (mean) bias2 (mean) bias3, over(survey) bar(1, fcolor($color2p2) lwidth(none)) bar(2, fcolor($color3plight) lwidth(none))	bar(3, fcolor($color3p2) lwidth(none)) bargap(1) blabel(bar, size(medsmall) format(%9.2f)) ytitle(Proportion of responses) ytitle(, size(medsmall)) yscale(noline) ylabel(0(.25)1, labsize(medsmall) angle(horizontal) format(%9.2f) tlength(zero) glcolor(gs12) gmin gmax)	title("{bf:A.} How biased is second-party punishment?", position(12) span size(medlarge) color(black)) subtitle("") caption("") note(" ") legend(order(1 "Biased" 2 "Neutral" 3 "Unbiased") rows(3) size(small) nobox region(lwidth(none)) position(2) yoffset(-4) ring(0)) plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black)) name(fig_q1_paper, replace) nodraw 	
graph bar (mean) unbiased1 (mean) unbiased2 (mean) unbiased3, over(survey) bar(1, fcolor($color2p2) lwidth(none)) bar(2, fcolor($color3plight) lwidth(none)) bar(3, fcolor($color3p2) lwidth(none)) bargap(1) blabel(bar, size(medsmall) format(%9.2f))	ytitle(Proportionof responses) ytitle(, size(medsmall)) yscale(noline) ylabel(0(.25)1, labsize(medsmall) angle(horizontal) format(%9.2f) tlength(zero) glcolor(gs12) gmin gmax) title("{bf:B.} Is one of the two types of punishment more biased?", position(12) span size(medlarge) color(black)) subtitle("") caption("") note(" ") legend(order(1 "2nd party" 2 "Equal" 3 "3rd party") rows(3) size(small) nobox region(lwidth(none)) position(2) yoffset(-4) ring(0)) plotregion(style(none)) graphregion(style(none)) subtitle(" ", nobox size(large) color(black)) name(fig_q4_paper, replace) nodraw			
graph combine fig_q1_paper fig_q4_paper, iscale(*1.25) imargin(sides) xsize(15) ysize(6) graphregion(margin(tiny) style(none)) name(fig_survey_bias, replace)	
// TESTS	
use survey1.dta, clear
keep bias_compare unbiased*
signrank bias_compare=0 
signrank unbiased1=unbiased3
signrank unbiased1=unbiased2
signrank unbiased2=unbiased3
signtest unbiased1=unbiased3
signtest unbiased1=unbiased2
signtest unbiased2=unbiased3
use survey2.dta, clear
keep bias_compare unbiased*
signrank bias_compare=0 
signrank unbiased1=unbiased3
signrank unbiased1=unbiased2
signrank unbiased2=unbiased3
signtest unbiased1=unbiased3
signtest unbiased1=unbiased2
signtest unbiased2=unbiased3
********************************************************************************
exit, clear
********************************************************************************
// Comments:
********************************************************************************
